package com.niit.session23;

//创建线程的方法2：实现Runnable接口
public class ThreadDemo2 implements Runnable {
//并重写run方法
	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println("子线程的名字："+name);
		for (int i = 1; i <= 100; i++) {
			System.out.println("子线程-->" + i);
			
			try {
//				Thread.sleep()可以让线程休眠指定的毫秒数
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
//		由于Runnable没有start方法，所以还需要一个Thread对象帮助线程启动
		ThreadDemo2 threadDemo2 = new ThreadDemo2();
		Thread thread = new Thread(threadDemo2);
		thread.start();

		// 使用lambda表达式快速创建线程并启动
		new Thread(() -> {
			for (int i = 1; i <= 100; i++) {
				System.out.println("子线程1-->" + i);
			}
		}).start();

		String name = Thread.currentThread().getName();
		System.out.println("主线程的名字："+name);
		for (int i = 1; i <= 100; i++) {
			System.out.println("主线程==》" + i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}
